A Step 您所在的位置:网站首页 admin page framework wp plugin directory A Step

A Step

2023-04-15 04:26| 来源: 网络整理| 查看: 265

According to W3Techs, WordPress powers 39.1% of all websites in 2020. This is because WordPress has a ton of built-in functionalities, but also because it is highly customizable through plugins. Plugins are a critical component of the WordPress platform, allowing you to easily extend functionality that goes beyond the WordPress core, without modifying it. 

For example,  WooCommerce converts your WordPress site into an eCommerce store, enabling you to sell products and services to your clients through the web. Yoast SEO is a plugin that helps you manage your SEO to rank your posts higher on search engines.

While developers can list their plugins on the web for anyone to use, these plugins may not solve your specific requirements. There are almost sixty thousand plugins on the official WordPress plugin directory, which means you should check if an existing plugin matches your requirements, before building your own. If there isn’t one that matches your needs, you should consider creating a custom WordPress plugin.

In the background, scripting and query languages like PHP and SQL power WordPress. Therefore, expertise in these technologies, as well as experience with deployment, are necessary to create a WordPress plugin.

In this tutorial, we will guide you through the creation of your first plugin.

Table of contentsWordPress Plugin Development: Basic ConceptsHooksShortcodesWidgetsWordPress Plugin Development: Key StepsStep 1 – Define the RequirementsStep 2 – Create a WordPress Plugin Directory StructureStep 3 – Configure your PluginStep 4 – Add Functionality to your PluginStep 5 – Package your PluginWordPress Plugin Development: Best PracticesBonus tips from Codeable plugin development expertsFinal Thoughts on WordPress Plugin DevelopmentFrequently Asked Questions WordPress Plugin Development: Basic Concepts

When WordPress gets updated to a new version, it overrides its core files. Because of this, if you add a custom functionality to a WordPress site by directly modifying the WordPress core, your changes will be wiped out upon WordPress upgrade. This leads to one of the core WordPress development concepts – any functionality you want to add or modify should be done using plugins.

A WordPress plugin is essentially one or more functions defined in PHP files, as PHP is the main scripting language powering WordPress. It also typically has 3 other components:  hooks (action hooks and filter hooks), shortcodes and widgets. These are the main elements of WordPress plugin development.

Hooks

Hooks are features in WordPress that allow you to manipulate a process at a specific point without changing WordPress core files. Therefore, hooks provide a way for your plugin to attach to the working of WordPress core. You can associate code snippets or functions with hooks to execute at various points in time. A hook can be applied both to action (action hook) and filter (filter hook). 

Before we dive in, let’s quickly cover a few differences between these two: 

An action is a process in WordPress. An action hook allows you to add a process. You can work with action using the add_action() function in WordPress. Common examples of actions are creating, reading, or saving a post in WordPress. You can associate PHP functions or code snippets with actions. You can even create your own action and associate code with it. An action enables you to add functionality to a plugin.

You can hook on to an action and run your custom functionality. For instance, you can associate your function, custom_function() to the action of saving a post. You should use the add_action() function as shown below.

function custom_function( $post_id ) { //do something } add_action( 'save_post','custom_function' );

This code snippet ensures that every time WordPress saves a post, the function, custom_function(), will run.

A filter is a hook that modifies a process. Filters help in manipulating existing data, without the need to alter its source. You can use the apply_filters() function to use a filter hook. It takes two required arguments – the name of the filter and the value that will be filtered.

echo apply_filters( 'filter_name','filter_variable' );

Further, you can use the add_filter() function to create a custom filter. You define the name of the filter you want to call, along with the function that will be called to modify the filter.

function modify_value( $filter ) { //change $filter and return new value } add_filter( 'original_filter','modify_value' ); Shortcodes

When you develop a plugin, it does not directly have access to the WordPress theme. To communicate with the WordPress theme and display some information to the user, you need to use a shortcode. Shortcodes allow users to insert a dynamic HTML element into a post or a page.

Here is an example of a shortcode which adds the text “Hello World” to your post.

//Register shortcode add_shortcode( 'hello_world_shortcode','hello_world_output' ); //define function to show output function hello_world_output( $atts, $content = '', $tag ){ $html = ''; $html .= 'Hello World'; return $html; } Widgets

Widgets provide developers another way to display your plugin’s content to the end-user. WordPress has a WP_widget class in PHP, that you need to extend to create a widget for your plugin.

Now that we covered the basic concepts of WordPress plugin development, let’s explore the key steps in creating a custom plugin.

WordPress Plugin Development: Key Steps Step 1 – Define the Requirements

The first step in WordPress plugin development is to clearly define your development needs. Before you start, ensure that you have a clear idea of the objective of the plugin. When you have an accurate picture of the issue to resolve, you are able to execute your idea into an efficient plugin.

There are many factors that you can consider in this step. What are the features of this plugin? How are you going to customize it? What will the design look like?

Make sure to answer these questions because this step is linked to all the other steps of the process.

In this specific example, we are going to create a Hello World plugin. We will use this simple, bare-bones plugin as an example to illustrate the steps that you should follow to create a WordPress plugin.

Step 2 – Create a WordPress Plugin Directory Structure

The default WP directory for storing plugin code in the back end is /wp-content/plugins/. How you structure your plugin within this directory will depend on the complexity of the plugin. The name of the directory is the same as your plugin name, in lowercase and dashes in place of spaces. 

We recommend having a single PHP file that contains all the code of the plugin (/wp-content/plugins/my-plugin/my-plugin.php). Such a structure is ideal for a simple plugin that serves a small function.

If you plan to work with a plugin that has a lot of assets, you can organize your plugin based on the function of the code and PHP files. You can create directories such as assets for CSS and JavaScript files, i18n for localization files, templates, and widgets.

For more complex plugins, you can create an MVC view, with directories for model, view, and controller within the my-plugin directory. This helps in debugging later in a shorter time. In our simple and straightforward example of the Hello World plugin, we will create the hello-world directory with a single PHP file, hello-world.php inside it.

Step 3 – Configure your Plugin

Once you create your plugin directory and add files within it, you then need to add the file header. The file header is a PHP comment block that contains information about the plugin. You can find the contents of a sample file header in the WordPress codex.

After adding the file header, it will appear in the list of plugins on your WordPress admin.

The following lines constitute the plugin headers and go to the top of the plugin file.

Step 4 – Add Functionality to your Plugin

While you have created an empty plugin, it does not accomplish anything yet. You need to add functionality to it now. The plugin handbook of WordPress should serve as a guide. This is the step where you bring your idea to life.

In our simple plugin example, we will create a WordPress admin page with the text “Hello World” in it. When you add an admin page, it also adds a menu item.



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有